package us.mmapp;

import java.util.ArrayList;
import java.util.HashMap;

import us.mmapp.entities.FriendEntity;
import us.mmapp.entities.ObjectEntity;
import us.mmapp.interfaces.PhotoDownloaderListener;
import us.mmapp.services.PhotoDownloader;
import us.mmapp.util.ApplicationData;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

public class ClusterGalleryActivity extends Activity implements PhotoDownloaderListener
{
	private ArrayList<ObjectEntity> mObjects;
	private HashMap<String, ImageView> mThumbMap;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.cluster_gallery);
	    mObjects = OnMapActivity.lastCluster;

	    if (mObjects == null || mObjects.size() == 0)
	    	finish();
		Log.v("GALLERY", mObjects.size() + "");
	    Gallery g = (Gallery) findViewById(R.id.gallery);
	    g.setAdapter(new ClusterAdapter(mObjects));
	    g.setOnItemClickListener(new OnItemClickListener()
	    {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				  // it's an individual
				  Intent intent = new Intent(ClusterGalleryActivity.this, CommentActivity.class);
				  Bundle bundle=new Bundle();
				  bundle.putInt("keyword", (int) id);
				  intent.putExtras(bundle);
				  ClusterGalleryActivity.this.startActivity(intent);
			}
		});
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		if (mThumbMap == null)
		{
			mThumbMap = new HashMap<String, ImageView>();
		}
		
		PhotoDownloader.attach(this);
	}
	
	@Override
	public void onPause()
	{
		super.onPause();
		PhotoDownloader.deattach(this);
	}
	class ClusterAdapter extends BaseAdapter 
	{
		private ArrayList<ObjectEntity> mAdaptedObjects;
		private View[] mViews;
		public ClusterAdapter(ArrayList<ObjectEntity> objects)
		{
			super();
			mAdaptedObjects = objects;
			mViews = new View[objects.size()];
		}
		@Override
		public int getCount()
		{
			return mAdaptedObjects.size();
		}

		@Override
		public Object getItem(int position)
		{
			return mAdaptedObjects.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			ObjectEntity o = (ObjectEntity) getItem(position);
			return o.getId();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View myView = mViews[position];
			if (myView == null)
			{
				LayoutInflater inflater = getLayoutInflater();
				myView = inflater.inflate(R.layout.gallery_item, null);
			
				ObjectEntity obj = (ObjectEntity)getItem(position);
			
				TextView titleView = (TextView) (myView.findViewById(R.id.gallery_item_title));
				titleView.setText(obj.getTitle().subSequence(0, Math.min(20, obj.getTitle().length())));
				TextView dateView = (TextView) (myView.findViewById(R.id.gallery_item_date));
				dateView.setText(us.mmapp.util.TextUtils.convertDate(obj.getDateCreated()));
				ImageView img =  (ImageView) (myView.findViewById(R.id.imageView1));
				
				mThumbMap.put(obj.getUrl_thumb(), img);
				FriendEntity x = ApplicationData.getFriends().get(obj.getUid());
				
				if (x != null && obj.getUid() != ApplicationData.getUserId())
				{
					TextView author = (TextView) (myView.findViewById(R.id.gallery_item_author));
					author.setText("@" + x.getUsername());
				}
				
				if (obj.getType() == 0)
					img.setImageDrawable(getResources().getDrawable(R.drawable.photo_ballon));
				if (obj.getType() == 1)
					img.setImageDrawable(getResources().getDrawable(R.drawable.video_ballon));
				if (obj.getType() == 2)
					img.setImageDrawable(getResources().getDrawable(R.drawable.audio_ballon));
				
				Bitmap photo = null;
				
				if (obj.getLocalThumb() != null && obj.getLocalThumb() != "")
				{
					photo = BitmapFactory.decodeFile(obj.getLocalThumb());
				}
				else
				{
					photo = PhotoDownloader.getPhoto(obj.getUrl_thumb());
				}
				if (photo != null)
				{
					img.setImageBitmap(photo);
				}
			}
			return myView;
		}
	}
	
	@Override
	public void available(String location, Bitmap photo)
	{
		if (photo != null)
		{
			ImageView iv = mThumbMap.get(location);
			
			if (iv != null)
			{
				iv.setImageBitmap(photo);
			
				Gallery g = (Gallery) findViewById(R.id.gallery);
				ClusterAdapter ca = (ClusterAdapter)g.getAdapter();
				ca.notifyDataSetChanged();
			}
		}
	}
}
